package course.controller;

import com.alibaba.excel.EasyExcel;
import course.pojo.entity.*;
import course.pojo.excelModel.*;
import course.service.*;
import course.service.impl.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 批量导出为excel
 *
 * @author HLF
 * @date 2022/7/5 16:37
 */
@WebServlet("/admin/bathExportController")
public class BathExportController extends BaseController {
    private CollegeService collegeService = new CollegeServiceImpl();

    private StudentService studentService = new StudentServiceImpl();

    private ZhuanyeService zhuanyeService = new ZhuanyeServiceImpl();

    private SubjectService subjectService = new SubjectServiceImpl();

    private TeacherService teacherService = new TeacherServiceImpl();

    protected void student(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        String eall = req.getParameter("eall");

        //获取StudentModel的list集合
        List<StudentModel> collect = getStudentModels(req, eall);

        String fileName = URLEncoder.encode("学生列表", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename= " + fileName + ".xlsx");

        EasyExcel.write(resp.getOutputStream(), StudentModel.class).sheet("学生列表").doWrite(collect);

    }

    /**
     * @param req  请求对象
     * @param eall 如果为空表示批量导出选中，不为空表示导出所有
     * @return
     */
    private List<StudentModel> getStudentModels(HttpServletRequest req, String eall) {
        String[] ids;
        List<Student> students;
        if (StringUtils.isNotBlank(eall)) {
            students = studentService.getList();
        } else {
            ids = req.getParameterValues("selected");
            List<String> strings = Arrays.asList(ids);
            List<Integer> integers = strings.stream().map(Integer::valueOf).collect(Collectors.toList());
            students = studentService.selectBathByIds(integers);
        }

        List<StudentModel> collect = students.stream().map(student -> {
            StudentModel studentModel = new StudentModel();
            try {
                BeanUtils.copyProperties(studentModel, student);
                Zhuanye zhuanye = zhuanyeService.getById(student.getZid());
                studentModel.setZname(zhuanye.getZname());
                College college = collegeService.getById(zhuanye.getCid());
                studentModel.setCid(college.getCid());
                studentModel.setCname(college.getCname());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return studentModel;
        }).collect(Collectors.toList());
        return collect;
    }


    protected void college(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        String eall = req.getParameter("eall");
        String[] ids;
        List<College> colleges;
        if (StringUtils.isNotBlank(eall)) {
            colleges = collegeService.getList();
        } else {
            ids = req.getParameterValues("selected");
            List<String> strings = Arrays.asList(ids);
            List<Integer> integers = strings.stream().map(Integer::valueOf).collect(Collectors.toList());
            colleges = collegeService.selectBathByIds(integers);
        }

        List<CollegeModel> collect = colleges.stream().map(college -> {
            CollegeModel collegeModel = new CollegeModel();
            try {
                BeanUtils.copyProperties(collegeModel, college);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return collegeModel;
        }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("学院列表", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename= " + fileName + ".xlsx");

        EasyExcel.write(resp.getOutputStream(), CollegeModel.class).sheet("学院列表").doWrite(collect);
    }

    protected void subject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        String eall = req.getParameter("eall");
        String[] ids;
        List<Subject> subjects;
        if (StringUtils.isNotBlank(eall)) {
            subjects = subjectService.getList();
        } else {
            ids = req.getParameterValues("selected");
            List<String> strings = Arrays.asList(ids);
            List<Integer> integers = strings.stream().map(Integer::valueOf).collect(Collectors.toList());
            subjects = subjectService.selectBathByIds(integers);
        }


        List<SubjectModel> collect = subjects.stream().map(subject -> {
            SubjectModel subjectModel = new SubjectModel();
            try {
                BeanUtils.copyProperties(subjectModel, subject);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return subjectModel;
        }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("课程列表", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename= " + fileName + ".xlsx");

        EasyExcel.write(resp.getOutputStream(), SubjectModel.class).sheet("课程列表").doWrite(collect);
    }

    protected void zhuanye(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");


        String eall = req.getParameter("eall");
        String[] ids;
        List<Zhuanye> zhuanyes;
        if (StringUtils.isNotBlank(eall)) {
            zhuanyes = zhuanyeService.getList();
        } else {
            ids = req.getParameterValues("selected");
            List<String> strings = Arrays.asList(ids);
            List<Integer> integers = strings.stream().map(Integer::valueOf).collect(Collectors.toList());
            zhuanyes = zhuanyeService.selectBathByIds(integers);
        }

        List<ZhuanyeModel> collect = zhuanyes.stream().map(zhuanye -> {
            ZhuanyeModel zhuanyeModel = new ZhuanyeModel();
            try {
                BeanUtils.copyProperties(zhuanyeModel, zhuanye);
                College college = collegeService.getById(zhuanye.getCid());
                zhuanyeModel.setCname(college.getCname());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return zhuanyeModel;
        }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("专业列表", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename= " + fileName + ".xlsx");

        EasyExcel.write(resp.getOutputStream(), ZhuanyeModel.class).sheet("专业列表").doWrite(collect);
    }

    protected void teacher(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        String eall = req.getParameter("eall");
        String[] ids;
        List<Teacher> teachers;
        if (StringUtils.isNotBlank(eall)) {
            teachers = teacherService.getList();
        } else {
            ids = req.getParameterValues("selected");
            List<String> strings = Arrays.asList(ids);
            List<Integer> integers = strings.stream().map(Integer::valueOf).collect(Collectors.toList());
            teachers = teacherService.selectBathByIds(integers);
        }


        List<TeacherModel> collect = teachers.stream().map(teacher -> {
            TeacherModel teacherModel = new TeacherModel();
            try {
                BeanUtils.copyProperties(teacherModel, teacher);
                College college = collegeService.getById(teacher.getCid());
                teacherModel.setCname(college.getCname());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return teacherModel;
        }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("教师列表", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename= " + fileName + ".xlsx");

        EasyExcel.write(resp.getOutputStream(), TeacherModel.class).sheet("教师列表").doWrite(collect);
    }
}
